#include <stdio.h>
#include <stdlib.h>
#include <windows.h>

#include "PoolJob.h"
//#include "parser.h"
#include "Communicator.h"

//#include "dumpcode.h"


CPJNonControl::CPJNonControl()
{
	m_pHttpComm = NULL;
	memset(&m_EngineData, 0, sizeof(m_EngineData));
}

CPJNonControl::CPJNonControl(CPJNonControl &job)
{
	m_pHttpComm = NULL;
	m_EngineData = job.m_EngineData;
}

ENGINE_DATA CPJNonControl::GetEngineData()
{
	return m_EngineData;
}

void CPJNonControl::SetEngineData(ENGINE_DATA ed)
{
	m_EngineData = ed;
}

/*
 * Job function to be used by the thread pool
 */
unsigned CPJNonControl::RunningJob()
{
	// There are no information to determine which derived classes are allocated to.
	// So, every connection from a client to a proxy use CNormalHttpComm class that indicate a behavior of Normal HTTP protocol
	// without any extension mechanism.
	// In any case, other derived class will be also allocated to it again when other connection mechanism is needed.
	m_pHttpComm = new CNormalHttpComm(&m_EngineData);

	MESSAGE_PAIR msg;
	msg.bComplete = FALSE;

	if(m_pHttpComm->ReqRecv(&msg.pReq) == COMM_ERROR)
		fprintf(stderr, "ReqRecv ERROR");
	if(m_pHttpComm->ReqSend(&msg.pReq) == COMM_ERROR)
		fprintf(stderr, "ReqSend ERROR");
	if(m_pHttpComm->ResRecv(&msg.pRes) == COMM_ERROR)
		fprintf(stderr, "ResRecv ERROR");
	if(m_pHttpComm->ResSend(&msg.pRes) == COMM_ERROR)
		fprintf(stderr, "ResSend ERROR");
	
	delete m_pHttpComm;
	m_pHttpComm = NULL;

	return 0;
/*
	// m_httpProxyData should have address information of three hosts(client, proxy) and socket number
	// between client and proxy.
	httpComm.SetHttpProxyData(&(m_EngineData.HttpProxyData));

	if(m_EngineData.ProxyInfo.mode == PM_NONCONTROL) {
		// receive a request from client. content is stored in httpComm instance.
		if(httpComm.GetCommunicateState() == CS_READY_TO_RECV_REQ) {
			if(!httpComm.readProxyHttpRequest())
				return 1;
		} else
			return 1;

		// send a request to web server.
		if(httpComm.GetCommunicateState() == CS_READY_TO_SEND_REQ) {
			if(!httpComm.sendProxyHttpRequest())
				return 1;
		} else 
			return 1;

		// receive a response from web server
		if(httpComm.GetCommunicateState() == CS_READY_TO_RECV_RES) {
			if(!httpComm.readProxyHttpResponse())
				return 1;
		} else 
			return 1;

		// send a response to client.
		if(httpComm.GetCommunicateState() == CS_READY_TO_SEND_RES) {
			if(!httpComm.sendProxyHttpResponse())
				return 1;
		} else 
			return 1;
	}

		
	httpComm.CleanupHttpComm();

	return 0;
*/
}

CPJNonControl& CPJNonControl::operator =(const CPJNonControl &job)
{
	m_EngineData = job.m_EngineData;

	return *this;
}

